import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../viewmodel/MainPageViewModel.dart';

class AddCateGoryList extends StatefulWidget {
  const AddCateGoryList({super.key});

  @override
  State<AddCateGoryList> createState() => _AddCateGoryListState();
}

class _AddCateGoryListState extends State<AddCateGoryList> {
  final _mainPageViewModel = MainPageViewModel();

  void _gg() {
    // var firstLevelCategory = FirstLevelCategory(name: "xcxcx");
    // DatabaseHelper().insert(FirstLevelCategory(name: "xcxcx"));
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
    name();
  }

  Future<void> name() async {
    await _mainPageViewModel.getALLOneCategory();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          // AppBar 的标题
          title: const Text(
            '自定义分类',
            style: TextStyle(),
          ),
          centerTitle: true,
          actions: [
            InkWell(
              onTap: () {},
              child: const Text("添加"),
            )
          ],
        ),
        body: ReorderableListView(
          onReorder: (int oldIndex, int newIndex) {
            setState(() {
              if (newIndex > oldIndex) {
                newIndex -= 1;
              }
              final item = _mainPageViewModel.firList.removeAt(oldIndex);
              _mainPageViewModel.firList.insert(newIndex, item);
            });
          },
          children: List.generate(
            _mainPageViewModel.firList.length,
            (index) {
              return ListTile(
                key: Key('$index'),
                onTap: () {},
                title: Text(_mainPageViewModel.firList[index].name),
              );
            },
          ),
        ));
  }
}
